/*
Streets-ButtonedEdit By Jimmy
*/
if (typeof(Streets) == "undefined")
	Streets = { };

Streets.ButtonedEdit=function(elementId){
	var ajaxChannel = "streets-ButtonedEdit|d";
	var codeElementId = elementId+"_$Id";
	var iconElementId = elementId +"_$Icon";
		
    function initIconPosition(){
    	var icon = wicketGet(iconElementId);
    	var iconWidth = icon.offsetWidth;
    	var obj=wicketGet(elementId);    		
    	obj.style.paddingRight = iconWidth;
    	var objPos = getPosition(obj);
    	icon.style.visibility = "visible";
    	setPosition(icon,[objPos[0] + obj.offsetWidth - iconWidth - 1,objPos[1]+4]); 
    }
    
    function initialize(){        	
        var obj=wicketGet(elementId);

        var initIcon = function (){
        	var icon = wicketGet(iconElementId); 
        	initIconPosition();
        	icon["initPos"] = initIconPosition;
            icon.onclick=function(event){
                Wicket.$(elementId).focus();
                killEvent(event);
                var clickUrl = this.getAttribute("clickUrl");
               	wicketAjaxGet(clickUrl);
            }
        }
        if (obj.offsetWidth > 0){
        	initIcon();
        } else {
        	window.setTimeout(initIcon, 200);
        }
    }

    function killEvent(event){
        if(!event)event=window.event;
        if(!event)return false;
        if(event.cancelBubble!=null){
            event.cancelBubble=true;
        }
        if(event.returnValue){
            event.returnValue=false;
        }
        if(event.stopPropagation){
            event.stopPropagation();
        }
        if(event.preventDefault){
            event.preventDefault();
        }
        return false;
    }

	 function getPosition(obj) {
		var box = obj.getBoundingClientRect();
	    var doc = obj.ownerDocument;
	    var scrollLeft =Math.max(doc.documentElement.scrollLeft,doc.body.scrollLeft);
	    var scrollTop = Math.max(doc.documentElement.scrollTop,doc.body.scrollTop);
	    var xy = [box.left, box.top];
	        
	    if (Wicket.Browser.isIE()){
	        var index = navigator.userAgent.indexOf("MSIE");
	    	var version = parseFloat(navigator.userAgent.substring(index + 5));
	    	if (version<8){
	    		var off1=2;
	    		var off2 =2;
	    		var bLeft = getStyle(doc.documentElement,"borderLeftWidth");
	    		var bTop = getStyle(doc.documentElement,"borderTopWidth");
	            if (bLeft !== "medium") off1 = parseInt(bLeft, 10);
	            if (bTop !== "medium") off2 = parseInt(bTop, 10);
	            xy[0] -= off1;
	            xy[1] -= off2;
	        }            
	    }
	    if ((scrollTop || scrollLeft)) {
	       xy[0] += scrollLeft;
	       xy[1] += scrollTop;
	    }
	    return xy;	    
    }
	 
	function setPosition(obj,xy,noRetry){
		var delta = [parseInt(getStyle(obj,"left"), 10), parseInt(getStyle(obj,"top"), 10 )];
		if ( isNaN(delta[0]) ) delta[0] = obj.offsetLeft;
		if ( isNaN(delta[1]) ) delta[1] = obj.offsetTop;
		var currentXY = getPosition(obj);

		if (xy[0] !== null) obj.style.left = xy[0] - currentXY[0] + delta[0] + 'px';
		if (xy[1] !== null) obj.style.top = xy[1] - currentXY[1] + delta[1] + 'px';
		if (!noRetry){
			var newXY = getPosition(obj);
			if ( (xy[0] !== null && newXY[0] != xy[0]) || 
				(xy[1] !== null && newXY[1] != xy[1]) ) {
				setPosition(obj, xy,true);
			}
		}
	}
    
    // From http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
    function getStyle(obj,cssRule) {
    	var cssRuleAlt = cssRule.replace(/\-(\w)/g,function(strMatch,p1){return p1.toUpperCase();});
        var value=obj.style[cssRuleAlt];
        if (!value) {
	        if (document.defaultView && document.defaultView.getComputedStyle) {
	            value = document.defaultView.getComputedStyle(obj,"").getPropertyValue(cssRule);
	        }
	        else if (obj.currentStyle)
	        {
	            value=obj.currentStyle[cssRuleAlt];
	        }
        }
        return value;
    }

    initialize();
}
